$(function () {
    // 点击去注册连接
    $('#register_link').on('click', function () {
        $('.login_box').hide()
        $('.login_window').css("height", "320px")
        $('.register_box').show()

    })
    // 点击去登录连接
    $('#login_link').on('click', function () {
        $('.login_box').show()
        $('.login_window').css("height", "270px")
        $('.register_box').hide()
    })

    let form = layui.form
    let layer = layui.layer;
    form.verify({

        pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位,且不能出现空格'
        ],

        repass: function (value) {
            let pass = $('.register_box [name=password]').val()

            if (pass !== value) {
                return '密码不相同'
            }
        }
    });

    /* $('.login_box .layui-form').on('submit',function(){
        
    }) */
    $('.register_box .layui-form').on('submit', function (e) {
        e.preventDefault();
        let data = {
            "username": $(".register_box [name=username]").val(),
            "password": $(".register_box [name=password]").val()

        }
        $.post('/api/reguser', data, function (res) {
            if (res.status !== 0) {
                return layer.msg('注册失败');

            }

            layer.msg('注册成功');
            $('.login_box').show()
            $('.login_window').css("height", "270px")
            $('.register_box').hide()
        })

    })
    $('.login_box .layui-form').on('submit', function (e) {
        e.preventDefault();
        $.post('/api/login', $(this).serialize(), function (res) {


            if (res.status !== 0) {
                return layer.msg('登陆失败');

            }

            layer.msg('登录成功');
            window.localStorage.setItem("token", res.token)
            location.href = './index.html'

        })

    })
})